<?php
/*
 * @ControllerAnnotation(title="客户管理")
 */

namespace app\crm\controller\customer;

use app\common\controller\CrmController;
use app\common\service\BaseService;
use app\crm\model\CrmCustomer;
use app\crm\model\CrmRecord;
use app\crm\traits\Curd;

class Index extends CrmController
{
    protected $validate = 'customer\Customer';
    protected $model = 'CrmCustomer';

    use Curd;

    public function initialize()
    {
        parent::initialize();
        if( in_array($this->request->action(),['add','edit','index']) ){
            $this->assign('levelList', BaseService::getLevelList());// 校区
            $this->assign('industryList', BaseService::getIndustryList());  // 专业
            $this->assign('sourceList', BaseService::getSourceList());  // 专业
        }
    }

    /**
     * @NodeAnotation(title="列表")
     */    
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $where[] =  ['owner_user_id', '<>', 0];
            $count = $this->model::with('level,industry,source,createUser,ownerUser')
                ->where($where)
                ->count();
            $list = $this->model::with('level,industry,source,createUser,ownerUser')
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

        /**
     * @NodeAnotation(title="回收站")
     */    
    public function recycle()
    {
        if ($this->request->isAjax()) {
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model::onlyTrashed()->with('level,industry,source,createUser,ownerUser')
                ->where($where)
                ->count();
            $list = $this->model::onlyTrashed()->with('level,industry,source,createUser,ownerUser')
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                //->fetchSql()
                ->select();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    /**
     * 跟进
     */
    public function record()
    {
        $id = $this->request->param('id');
        if (empty($id)) {
            return $this->error('缺少id参数');
        }
        if ($this->request->isAjax()) {
            list($page, $limit, $where) = $this->buildTableParames();
            $count = CrmRecord::with('createUser')->where('types_id',$id)->where($where)
                ->count();
            $list = CrmRecord::with('createUser')->where('types_id',$id)->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        $this->assign('customer',CrmCustomer::get($id));
        $this->assign('record_methods',BaseService::getRecordMethodList());
        return $this->fetch();
    }

}